import http from '../../utils/request'
var utils = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        indicatorDots: false, //小点
        indicatorColor: "white",//指示点颜色
        activeColor: "coral",//当前选中的指示点颜色
        autoplay: false, //是否自动轮播
        interval: 1000, //间隔时间
        duration: 1000, //滑动时间
        vertical:true,//滑动方向
        username:'',//用户名字
        dayNumber:'', // 天数
        downNumber:'', //下载次数
        markNumber:'', //组卷次数
        uploadNumber:'', //上传次数
        intergralNumber:'', //存有积分
        intergralRecord:'', //积分记录条数
        keywords:[], //关键词
        isPlayingMusic:true,
        activeNmumber:0,
        year:'',
        month:'',
        dayNu:'',
        scrollTure:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
         //判断是否登录
      let that = this
          let userInfo = wx.getStorageSync('userInfo') || null
          if(userInfo==null){
            this.setData({isShow:true})
          }else{
            var old_date = JSON.parse(userInfo).createDate; //可以暂时设定一个类似这样的值
            var oldT = old_date.replace(/-/g,"/");   // 解决在ios 中出现NAN 的问题 
            var new_date = Date.parse(new Date()) //新建一个日期对象，默认现在的时间
            var old_date = Date.parse(new Date(oldT)); //设置过去的一个时间点，"yyyy-MM-dd HH:mm:ss"格式化日期   
            var diffDate = Math.abs(new_date - old_date) // 取相差毫秒数的绝对值
            var totalDays = Math.floor(diffDate / (1000 * 3600 * 24)) // 向下取整
                this.setData({username:JSON.parse(userInfo).wxname,dayNumber:totalDays,intergralNumber:JSON.parse(userInfo).myIntegral,activeNmumber:0,year:JSON.parse(userInfo).createDate.split('-')[0],month:JSON.parse(userInfo).createDate.split('-')[1],dayNu:JSON.parse(userInfo).createDate.split('-')[2].split(' ')[0]})
                //下载次数
                // this.downNumberTap(JSON.parse(userInfo).id,JSON.parse(userInfo).authorization)
                // //组卷次数
                // this.markNumberTap(JSON.parse(userInfo).id,JSON.parse(userInfo).authorization)
                // //上传数量
                // this.uploadNumberTap(JSON.parse(userInfo).id,JSON.parse(userInfo).authorization)
                // // 积分明细
                // this.recordNumberTap(JSON.parse(userInfo).id,JSON.parse(userInfo).authorization)
                // //关坚持
                // this.keywordsNumberTap(JSON.parse(userInfo).id,JSON.parse(userInfo).authorization)
                // //音乐播放
                // wx.playBackgroundAudio({
                //     dataUrl: 'https://oss.bslmedu.com/wechart/audioReport.mp3',
                // })
          }
    },
    //swiper滑动页数
    swiperActive(e){
        this.setData({activeNmumber:e.detail.current})
    },
    //控制音乐播放器
    // onmusicTap: function (event) {
    //     if(this.data.isPlayingMusic){
    //         wx.pauseBackgroundAudio()
    //         this.setData({isPlayingMusic:false})
    //     }else{
    //         wx.playBackgroundAudio({
    //           dataUrl: 'https://oss.bslmedu.com/wechart/audioReport.mp3',
    //         })
    //         this.setData({isPlayingMusic:true})
    //     }
    //   },
    // 下载次数
    async downNumberTap(id,authorization){
        let that = this
        let options={
                url:'/getRecord',
                method:'POST',
                data:{page:1,pagesize:1,userid:id,authorization:authorization},
                header:{"content-type":"application/json;charset=UTF-8"}
          }
          let res = await http(options)
          if(res.data.code==0){
              this.setData({downNumber:res.data.obj.count})
          }
      },
      //我的组卷记录
      async markNumberTap(id,authorization){
        let that = this
        let options={
                url:'/getMyPapers',
                method:'POST',
                data:{page:1,pagesize:1,userId:id,authorization:authorization},
                header:{"content-type":"application/json;charset=UTF-8"}
          }
          let res = await http(options)
          if(res.data.code==0){
              this.setData({markNumber:res.data.obj.count})
          }
      },
      //上传资源数量getMyItemCases
      async uploadNumberTap(id,authorization){
        let that = this
        let options={
                url:'/getMyItemCases',
                method:'POST',
                data:{page:1,pagesize:1,userId:id,authorization:authorization},
                header:{"content-type":"application/json;charset=UTF-8"}
          }
          let res = await http(options)
          if(res.data.code==0){
              this.setData({uploadNumber:res.data.obj.count})
          }
      },
      //积分记录条数
      async recordNumberTap(id,authorization){
        let that = this
        let options={
                url:'/getRecord2',
                method:'POST',
                data:{page:1,pagesize:1,userid:id,authorization:authorization},
                header:{"content-type":"application/json;charset=UTF-8"}
          }
          let res = await http(options)
          if(res.data.code==0){
              this.setData({intergralRecord:res.data.obj.count})
          }
      },
      //关键词
      async keywordsNumberTap(id,authorization){
        let that = this
        let {downNumber,uploadNumber} = this.data
        let options={
                url:'/getUserLabel',
                method:'POST',
                data:{userId:id,authorization:authorization},
                header:{"content-type":"application/json;charset=UTF-8"}
          }
          let res = await http(options)
          console.info(res)
          if(res.data.code==0){
              if(res.data.obj){
                var stuCount  = res.data.obj;
                this.resetDataFun(stuCount,4)
              }else{
                if(downNumber=='' || uploadNumber==''){
                    var stuCount  = ['观望']
                    this.resetDataFun(stuCount,4)
                }else if(downNumber==''&&uploadNumber.length>0){
                    var stuCount  = ['活雷锋']
                    this.resetDataFun(stuCount,4)
                }
              }
          }
      },
      resetDataFun(stuCount,col){
        if (stuCount.length>0){
            var objList = new Object();
            var arr = new Array();
            // var cow = stuCount.length / col == 0 ? stuCount.length / col : Math.ceil((stuCount.length / col));
            var cow = 3
            console.log('行数',cow);
            for (var i = 1; i <= cow; i++) {
                var tempArr = [];
                var temp;
                if (col * i > stuCount.length) {
                    temp = stuCount.length;
                } else {
                    temp = col * i;
                }
                for (var j = col * (i - 1); j < temp; j++) {
                    tempArr.push(stuCount[j]);
                }
                var newObj = new Object();
                newObj.data = tempArr
                arr.push(newObj);
          }
            objList.data = arr;
            this.resetData = objList;
            console.log(objList.data);
            this.setData({keywords:objList.data})
        }
    },
    scroll(event){
        this.setData({scrollTure:1})

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})